package com.ghn.cocknovel.utils;

import android.annotation.SuppressLint;
import android.os.CountDownTimer;


public class TimeTaskUtils {
    private static final String TAG = "tag";
    private CountDownTimer mTimer;
    private int defaultOverTime = 1000;

    @SuppressLint("SimpleDateFormat")
    public TimeTaskUtils() {
    }

    public TimeTaskUtils(int defaultOverTime) {
        this.defaultOverTime = defaultOverTime;
    }

    public void setDefaultOverTime(int defaultOverTime) {
        this.defaultOverTime = defaultOverTime;
    }

    public TimeTaskUtils runTime(long millisInFuture) {
        runTime(millisInFuture, defaultOverTime);
        return this;
    }

    public TimeTaskUtils runTime(long millisInFuture, long countDownInterval) {
        if (mTimer != null) {
            mTimer.cancel();
        }
        mTimer = new CountDownTimer(millisInFuture, countDownInterval) {

            @Override
            public void onTick(long millisUntilFinished) {
                if (changeListener != null) {
                    changeListener.onTimerChange(millisUntilFinished);
                }
            }

            @Override
            public void onFinish() {
                if (changeListener != null) {
                    changeListener.onTimerChange(0);
                    changeListener.onTimerOver();
                }
            }
        };
        mTimer.start();
        return this;
    }

    public void cancel() {
        if (mTimer != null) {
            mTimer.cancel();
        }
    }

    private OnTimerChangeListener changeListener;

    public TimeTaskUtils setChangeListener(OnTimerChangeListener changeListener) {
        this.changeListener = changeListener;
        return this;
    }

}
